package com.example.base.job;

import com.alibaba.fastjson.JSONArray;
import com.example.base.system.domain.SysJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class AppJobModelServiceImpl implements AppJobModelService {

    @Autowired
    private JobAppMap jobAppMap;

    @Autowired
    private QuarztUtils quarztUtils;

    @Override
    public void startJob(JobModel job) {
        log.info("jobName:{}",job.getJobname());
        quarztUtils.addQuartz(job);
    }

    @Override
    public void updateJob(JobModel job) {
        quarztUtils.stopQuarzt(job);
        quarztUtils.deleteQuarzt(job);
        quarztUtils.addQuartz(job);
    }

    @Override
    public void appJobExecute(JobModel jobModel) {
        String appCLass=jobModel.getAppClass();
        String appName=jobModel.getAppName();
        IJob iJob=jobAppMap.getIJob(appName,appCLass);
        if(iJob!=null){
            IJobMap iJobMap=new IJobMap();
            if(StringUtils.isNotBlank(jobModel.getParams())){
                iJobMap.setParams(JSONArray.parseArray(jobModel.getParams()));
            }
            iJob.exec(iJobMap);
        }

    }



    @Override
    public JobModel sysJobToJobModel(SysJob sysJob) {
        return quarztUtils.sysJobToJobModel(sysJob);
    }
}
